package patrol.mobile.com.mobilepatrol.activity.toll;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.CommonListActivity;
import patrol.mobile.com.mobilepatrol.adapter.MyEvaluationAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.bean.EvaluationDetailBean;
import patrol.mobile.com.mobilepatrol.bean.OrgListBean;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.DatePickerUtil03;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.TimeUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.view.CallBack;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;

/**
 * Created by K on 2018/9/14.
 * des: 我的考核
 */
public class MyEvaluationActivity extends CommonListActivity implements CallBack<DateBean> {


    private TextView tvStart;
    private MySpinner mMySpinner01;
    private TextView mShengyuTv;
    private String currentTime = "";
    private List<EvaluationDetailBean> evaluationDetailBeans;
    private String orgId;

    @Override
    public void initView() {
        super.initView();
        View itemView = LayoutInflater.from(this).inflate(R.layout.activity_my_evaluation_top, null);
        flContent.addView(itemView);
        tvStart = itemView.findViewById(R.id.tv_start);
        mMySpinner01 = itemView.findViewById(R.id.mySpinner01);
        mShengyuTv = itemView.findViewById(R.id.tv_shengyu);
        tvStart.setText(TimeUtils.dateStringYM(System.currentTimeMillis()));
        mShengyuTv.setText("张三本月剩余20分");
    }

    @Override
    protected BaseQuickAdapter initAdapter() {
        return new MyEvaluationAdapter(this);
    }

    @Override
    protected String initTitleName() {
        return "我的考核";
    }

    @Override
    public void configAdapter() {
        super.configAdapter();
        getOrg();
    }

    private void getOrg() {
        HashMap<String, String> hashMap = new HashMap();
//        hashMap.put("objType", "分局,总局");
        hashMap.put("orgId", UserManager.getOrgNo());
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getOrgList);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(final String code, String response) {
                final OrgListBean orgListBean = new Gson().fromJson(response, OrgListBean.class);
                List<String> list = new ArrayList<>();
                for (OrgListBean.ListBean listBean : orgListBean.getList()) {
                    list.add(listBean.getOrgName());
                }
                mMySpinner01.setmDatas(list, new MySpinner.CallBack() {
                    @Override
                    public void onSelect(int position) {
                        requestData(currentTime);
                        orgId = orgListBean.getList().get(position).getId();
                    }
                });
                if (orgListBean.getList().size() == 0) return;
                requestData("");
            }
        });
    }

    private void requestData(String month) {
        if (TextUtils.isEmpty(orgId)) {
            orgId = UserManager.getOrgNo();
        }
        HttpSender sender = new HttpSender(this, baseUrl, Urls.GET_ORG_MY_ORG_POST_LIST);
        HashMap<String, String> map = new HashMap<>();
        map.put("userId", current_id);
        map.put("orgId", orgId);
        map.put("month", month);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser parser = new JsonParser();
                JsonObject asJsonObject = parser.parse(response).getAsJsonObject();
                JsonArray list = asJsonObject.getAsJsonArray("list");
                evaluationDetailBeans = GsonUtil.jsonToList(list.toString(), EvaluationDetailBean.class);
                mAdapter.updateItems(evaluationDetailBeans);
            }
        });
    }

    @Override
    public void setListener() {
        super.setListener();
        tvStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatePickerUtil03.getInstance().show(MyEvaluationActivity.this, MyEvaluationActivity.this);
            }
        });
        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //考核明细
                Intent intent = new Intent(MyEvaluationActivity.this, MyEvaluationDetailActivity.class);
                intent.putExtra("id", evaluationDetailBeans.get(position).getId());
                intent.putExtra("assessmentPost", evaluationDetailBeans.get(position).getAssessmentPost());
                intent.putExtra("position", position);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onBack(DateBean dateBean) {
        currentTime = dateBean.getmDate();
        tvStart.setText(currentTime);
        requestData(currentTime);
    }
}
